package com.cn.dao.impl;
import java.sql.SQLException;

import org.apache.commons.dbutils.handlers.BeanHandler;

import com.cn.dao.UserDao;
import com.cn.domain.User;
import com.cn.utils.DBUtils;
import com.cn.utils.SHA;
public class UserDaoImpl implements UserDao{
	//按照名字查找用户名字,email,identity,tel
	@Override
	public User queryUserByKey(String key,String value){
		User user=null;
		try {
			Object[] params= {value};
			System.out.println("key="+key+":value="+value);
			user=DBUtils.getQuryRunner().query("select * from HWUA_USER where "+key+"=?",new BeanHandler<User>(User.class),params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}
	//添加用户信息
	@Override
	public int addUser(User user){
		int row=0;
		//加密
		user.setHu_password(SHA.shaSecurity(user.getHu_password()));
		try {
			Object[] params= {user.getHu_user_name(),user.getHu_password(),user.getHu_sex(),user.getHu_identity_code(),user.getHu_email(),user.getHu_mobile(),user.getHu_address(),user.getHu_status()};
			row=DBUtils.getQuryRunner().update("insert into HWUA_USER values(SEQ_USER.nextval,?,?,?,to_date('"+user.getHu_birthday()+"','yyyy-MM-dd'),?,?,?,?,?)",params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return row;
	}
	
	//按照用户名和密码验证
	@Override
	public User queryUserByNameAndPwd(User user) {
		User u=null;
		//加密
		user.setHu_password(SHA.shaSecurity(user.getHu_password()));
		try {
			Object[] params= {user.getHu_user_name(),user.getHu_password()};
			u=DBUtils.getQuryRunner().query("select * from HWUA_USER where HU_USER_NAME=? and HU_PASSWORD=?",new BeanHandler<User>(User.class),params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return u;
	}
	
	//找回密码（按照name，eaiml，id进行更新密码）
	@Override
	public int updateNewPwd(User user) {
		int i=0;
		//加密
		user.setHu_password(SHA.shaSecurity(user.getHu_password()));
		try {
			Object[] params= {user.getHu_password(),user.getHu_user_name(),user.getHu_identity_code(),user.getHu_email()};
			i=DBUtils.getQuryRunner().update("update HWUA_USER set HU_PASSWORD=? where HU_USER_NAME=? and HU_IDENTITY_CODE=? and HU_EMAIL=?",params);
			System.out.println("i=="+i);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	
	//找回密码（按照name，eaiml，id进行更新密码）
	@Override
	public User queryUserByIdAndEmail(User user) {
		User u=null;
		try {
			Object[] params= {user.getHu_user_name(),user.getHu_identity_code(),user.getHu_email()};
			u=DBUtils.getQuryRunner().query("select * from HWUA_USER where HU_USER_NAME=? and HU_IDENTITY_CODE=? and HU_EMAIL=?",new BeanHandler<User>(User.class),params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return u;
	}
}
